<?php

namespace app\admin\controller\app;

use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="app_gpu_price")
 */
class GpuPrice extends AdminController
{

    use \app\admin\traits\Curd;

    /**
     * 导出文件的名称，支持中文，为空则获取模型名称
     *
     * @var string
     */
    protected $exportFileName = '显卡报价';

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\AppGpuPrice();

        $this->assign('select_list_type', $this->model::SELECT_LIST_TYPE, true);

        $this->assign('select_list_status', $this->model::SELECT_LIST_STATUS, true);
    }


    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {


            $search_type = $this->request->get('search_type', 'all');




            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();

            if ($search_type == 'mine') {
                $where[] = [
                    'user_id', '=', $this->sessionAdmin->id
                ];
            }

            $count = $this->model
                ->withJoin(['appGpu'], 'LEFT')
                ->where($where)
                ->count();
            $model_list = $this->model
                ->withJoin(['appGpu'], 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort);

            $field_option = $model_list->getOptions('field');

            $field_option[] = 'app_gpu_price.price / appGpu.cuda_count as cost_price';

            $model_list->setOption('field', $field_option);

            $list = $model_list->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }


    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $post['user_id'] = $this->sessionAdmin->id;
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="导出")
     */
    public function export()
    {
        list($page, $limit, $where) = $this->buildTableParames();


        $this->model = $this->model->withJoin(['appGpu'], 'LEFT');

        $field_option = $this->model->getOptions('field');

        $field_option[] = 'app_gpu_price.price / appGpu.cuda_count as cost_price';

        $this->model->setOption('field', $field_option);

        $fields = $this->request->param('fields', '{}', null);
        $image_fields = $this->request->param('image_fields', '{}', null);
        $select_fields = $this->request->param('select_fields', '{}', null);
        $date_fields = $this->request->param('date_fields', '{}', null);

        $fields = json_decode($fields, true);
        $image_fields = json_decode($image_fields, true);
        $select_fields = json_decode($select_fields, true);
        $date_fields = json_decode($date_fields, true);

        $content = \app\common\tools\ExportTools::excel($this->model, $where, $fields, $image_fields, $select_fields, $date_fields);

        $export_file_name = $this->exportFileName;

        if (empty($export_file_name)) {
            $export_file_name = $this->model->getName();
        }


        return download($content, $export_file_name . date('YmdHis') . '.xlsx', true);
    }
}
